package com.atguigu.yygh.hosp.controller;


import com.atguigu.yygh.common.exception.YyghException;
import com.atguigu.yygh.common.result.R;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.hosp.utils.MD5;
import com.atguigu.yygh.model.hosp.HospitalSet;
import com.atguigu.yygh.vo.hosp.HospitalSetQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * <p>
 * 医院设置表 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2021-08-26
 */
@Api(tags = "医院设置接口")
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
//@CrossOrigin  //允许跨域访问
public class HospitalSetController {

    @Autowired
    private HospitalSetService hospitalSetService;

    //  [1,2,3]
    @DeleteMapping("batchRemove")
    public R batchRemoveHospitalSet(@RequestBody List<Long> list) {

//        try {
//            //模拟异常
//            int i = 10/0;
//        }catch(Exception e) {
//            //抛出
//            throw new YyghException(20001,"执行自定义异常");
//        }

        hospitalSetService.removeByIds(list);
        return R.ok();
    }

    // 医院设置锁定和解锁
    @PutMapping("lockHospitalSet/{id}/{status}")
    public R lockHospitalSet(@PathVariable Long id,
                             @PathVariable Integer status) {
        //根据id查询医院设置信息
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        //设置状态
        hospitalSet.setStatus(status);
        //调用方法
        hospitalSetService.updateById(hospitalSet);
        return R.ok();
    }

    //修改
    @ApiOperation(value = "修改")
    @PostMapping("updateHospSet")
    public R update(@RequestBody HospitalSet hospitalSet) {
        hospitalSet.setUpdateTime(new Date());
        hospitalSetService.updateById(hospitalSet);
        return R.ok();
    }

    @ApiOperation(value = "根据ID查询医院设置")
    @GetMapping("getHospSet/{id}")
    public R getById(@PathVariable String id){

        HospitalSet hospitalSet = hospitalSetService.getById(id);
        return R.ok().data("item", hospitalSet);
    }

    //添加
    @ApiOperation(value = "添加")
    @PostMapping("saveHospSet")
    public R save(@RequestBody HospitalSet hospitalSet) {

        //签名秘钥
        Random random = new Random();
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis()+""+random.nextInt(1000)));

        boolean flag = hospitalSetService.save(hospitalSet);
        if(flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    //条件查询带分页
    @ApiOperation(value = "条件查询分页医院设置")
    @PostMapping("findConditionPage/{current}/{limit}")
    public R listConditionPage(@PathVariable Long current,
                               @PathVariable Long limit,
                               @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo) {
        //创建page对象
        Page<HospitalSet> page = new Page<>(current,limit);

        //条件封装
        String hosname = hospitalSetQueryVo.getHosname();
        String hoscode = hospitalSetQueryVo.getHoscode();

        QueryWrapper<HospitalSet> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(hosname)) {
            wrapper.like("hosname",hosname);
        }
        if(!StringUtils.isEmpty(hoscode)) {
            wrapper.eq("hoscode",hoscode);
        }

        //调用方法
        hospitalSetService.page(page,wrapper);

        //从page获取数据
        List<HospitalSet> list = page.getRecords();
        long total = page.getTotal();
        return R.ok().data("total",total).data("rows",list);
    }

    //医院设置分页
    @ApiOperation(value = "医院设置列表")
    @GetMapping("findPage/{current}/{limit}")
    public R listPage(@PathVariable Long current,
                      @PathVariable Long limit) {
        //创建page对象
        Page<HospitalSet> page = new Page<>(current,limit);

        //调用方法
        hospitalSetService.page(page,null);

        //从page获取数据
        List<HospitalSet> list = page.getRecords();
        long total = page.getTotal();

//        Map map = new HashMap();
//        map.put("total",total);
//        map.put("rows",list);
//        return R.ok().data(map);

        return R.ok().data("total",total).data("rows",list);
    }

    //http://localhost:8201/admin/hosp/hospitalSet/findAll
    //查询所有医院设置信息
//    @ApiOperation(value = "医院设置列表")
//    @GetMapping("findAll")
//    public List<HospitalSet> findAllHosp() {
//        //调用service方法
//        List<HospitalSet> list = hospitalSetService.list();
//        return  list;
//    }

    @ApiOperation(value = "医院设置列表")
    @GetMapping("findAll")
    public R findAllHosp() {
        //调用service方法
        List<HospitalSet> list = hospitalSetService.list();

//        R r = new R();
//        r.setSuccess(true);
//        r.setCode(20000);
//        r.setMessage("成功");
//
//        Map<String, Object> data = new HashMap<String, Object>();
//        data.put("items",list);
//        r.setData(data);

        return R.ok().data("items",list).message("操作成功");
    }

    @ApiOperation(value = "医院设置删除")
    @DeleteMapping("{id}")
    public R deleteHospSet(@ApiParam(name = "id", value = "医院设置ID", required = true) @PathVariable Long id) {
        //调用service方法  code review
        boolean flag = hospitalSetService.removeById(id);

        if(flag) {
            return R.ok();
        } else {
            return R.error();
        }

//        R r = new R();
//        if(flag) {

//            r.setSuccess(true);
//            r.setCode(20000);
//            r.setMessage("成功");
//        } else {
//            r.setSuccess(false);
//            r.setCode(20001);
//            r.setMessage("失败");
//        }
//
//        return r;
    }

    //医院设置删除
//    @ApiOperation(value = "医院设置删除")
//    @DeleteMapping("{id}")
//    public boolean deleteHospSet(@ApiParam(name = "id", value = "医院设置ID", required = true) @PathVariable Long id) {
//        //调用service方法
//        boolean flag = hospitalSetService.removeById(id);
//        return flag;
//    }

}

